package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"gowork/clientAndServerStream/message"
	"io"
	"time"
)

func main() {

	orderMap := map[string]*message.OrderRequest{
		"201907300001": &message.OrderRequest{OrderId: "201907300001", TimeStamp:time.Now().Unix()},
		"201907310001": &message.OrderRequest{OrderId: "201907310001", TimeStamp:time.Now().Unix()},
		"201907310002": &message.OrderRequest{OrderId: "201907310002", TimeStamp:time.Now().Unix()},
		"201907310003": &message.OrderRequest{OrderId: "201907310003", TimeStamp:time.Now().Unix()},
		"201907310004": &message.OrderRequest{OrderId: "201907310004", TimeStamp:time.Now().Unix()},
		"201907310005": &message.OrderRequest{OrderId: "201907310005", TimeStamp:time.Now().Unix()},
		"201907310006": &message.OrderRequest{OrderId: "201907310006", TimeStamp:time.Now().Unix()},
		"201907310007": &message.OrderRequest{OrderId: "201907310007", TimeStamp:time.Now().Unix()},
		"201907310008": &message.OrderRequest{OrderId: "201907310008", TimeStamp:time.Now().Unix()},
		"201907310009": &message.OrderRequest{OrderId: "201907310009", TimeStamp:time.Now().Unix()},
		"201907310010": &message.OrderRequest{OrderId: "201907310010", TimeStamp:time.Now().Unix()},
		"201907310011": &message.OrderRequest{OrderId: "201907310011", TimeStamp:time.Now().Unix()},
		"201907310012": &message.OrderRequest{OrderId: "201907310012", TimeStamp:time.Now().Unix()},
		"201907310013": &message.OrderRequest{OrderId: "201907310013", TimeStamp:time.Now().Unix()},
		"201907310014": &message.OrderRequest{OrderId: "201907310014", TimeStamp:time.Now().Unix()},
		"201907310015": &message.OrderRequest{OrderId: "201907310015", TimeStamp:time.Now().Unix()},
		"201907310016": &message.OrderRequest{OrderId: "201907310016", TimeStamp:time.Now().Unix()},
		"201907310017": &message.OrderRequest{OrderId: "201907310017", TimeStamp:time.Now().Unix()},
		"201907310018": &message.OrderRequest{OrderId: "201907310018", TimeStamp:time.Now().Unix()},
		"201907310019": &message.OrderRequest{OrderId: "201907310019", TimeStamp:time.Now().Unix()},
		"201907310020": &message.OrderRequest{OrderId: "201907310020", TimeStamp:time.Now().Unix()},
		"201907310021": &message.OrderRequest{OrderId: "201907310021", TimeStamp:time.Now().Unix()},
		"201907310022": &message.OrderRequest{OrderId: "201907310022", TimeStamp:time.Now().Unix()},
		"201907310023": &message.OrderRequest{OrderId: "201907310023", TimeStamp:time.Now().Unix()},
		"201907310024": &message.OrderRequest{OrderId: "201907310024", TimeStamp:time.Now().Unix()},
		"201907310025": &message.OrderRequest{OrderId: "201907310025", TimeStamp:time.Now().Unix()},
		"201907310026": &message.OrderRequest{OrderId: "201907310026", TimeStamp:time.Now().Unix()},
		"201907310027": &message.OrderRequest{OrderId: "201907310027", TimeStamp:time.Now().Unix()},
		"201907310028": &message.OrderRequest{OrderId: "201907310028", TimeStamp:time.Now().Unix()},
	}

	//1、Dail连接
	conn, err := grpc.Dial("localhost:8092", grpc.WithInsecure())
	if err != nil {
		panic(err.Error())
	}
	defer conn.Close()

	orderServiceClient := message.NewOrderServiceClient(conn)

	fmt.Println("客户端请求RPC调用：双向流模式")

	orderInfoClient, err := orderServiceClient.GetOrderInfos(context.Background())

	var index int = 1
	orders := make([]*message.OrderRequest, 0)
	for _, info := range orderMap {
		orders = append(orders, info)
		if index%5 == 0 {
			err = orderInfoClient.Send(&message.OrderList{OrderList:orders})
			orders = []*message.OrderRequest{}
			fmt.Println("列表：", orders)
			if err != nil {
				panic(err.Error())
			}

			go func() {
				for {
					orderInfo, err := orderInfoClient.Recv()
					if err == io.EOF {
						fmt.Println("读取结束")
						return
					}
					if err != nil {
						return
					}
					fmt.Println("读取到的信息：", orderInfo)
				}
			}()
		}
		index += 1
	}

	if len(orders) > 0 {
		err := orderInfoClient.Send(&message.OrderList{OrderList:orders})
		if err != nil{
			panic(err.Error())
		}
	}

	//关闭
	orderInfoClient.CloseSend()

	select {}

	//for {
	//	orderInfo, err := orderInfoClient.Recv()
	//	if err == io.EOF {
	//		fmt.Println("读取结束")
	//		return
	//	}
	//	if err != nil {
	//		return
	//	}
	//	fmt.Println("读取到的信息：", orderInfo)
	//}

}